package com.wisdytech.common.utils;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;

/**
 * <p>
 * Title: MD工具类
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2017
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author xubin
 * @version 1.0
 */

public class BaseUtil {
    /**
     * 
     * <p>
     * Description: 把jsonStr信息写入到response中
     * </p>
     * 
     * @param response 响应
     * @param jsonStr 需要写入的集合
     */
    public static void writeJsonStr(HttpServletResponse response, String jsonStr) {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.write(jsonStr);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
    
    /**
     * 
     * <p>Description: 获取唯一id</p>
     * @return
     */
    public static String getUUID(){
        return UUID.randomUUID().toString().replaceAll("\\-", "");
    }
    

    /**
     *
     * <p>Description: 判断是否为ajax请求</p>
     * @param request 请求对象
     * @return boolean
     */
    public static boolean isAjax(HttpServletRequest request){
        if(StringUtils.isBlank(request.getHeader("X-Requested-With"))){
            return false;
        }
        return  "XMLHttpRequest".equals(request.getHeader("X-Requested-With").toString()) ;
    }


    
}
